package com.fieldchain.ex.fa100.utils.net.inter;

import android.os.SystemClock;
import android.util.Log;

import com.fieldchain.ex.fa100.inter.SimpleCallBack;
import com.fieldchain.ex.fa100.inter.StringCallBack;
import com.fieldchain.ex.fa100.utils.net.tools.MainHandler;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/27
 */
public class MySocketListener extends WebSocketListener {
    private String send;
    private StringCallBack callBack;
    private boolean needRestart = true;
    private SimpleCallBack simpleCallBack;

    public MySocketListener(String send, StringCallBack callBack) {
        this.send = send;
        this.callBack = callBack;
    }

    public void setSimpleCallBack(SimpleCallBack simpleCallBack) {
        this.simpleCallBack = simpleCallBack;
    }

    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
        boolean isSend = webSocket.send(send);
        Log.e("webSocketd:", "onOpen    "+ send);
    }

    @Override
    public void onMessage(WebSocket webSocket, final String text) {
        super.onMessage(webSocket, text);
        MainHandler.getInstance().post(new Runnable() {
            @Override
            public void run() {
                try {
                    String result="";
                    JSONObject object=new JSONObject(text);
                    int code=object.getInt("code");
                    if(code==500){
                        result="500";
                    }else{
                        result=object.getString("data");
                    }
                    callBack.message(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Log.e("webSocketd:", "onMessage1    " + text);
    }

    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        super.onMessage(webSocket, bytes);
        Log.e("webSocketd:", "onMessage2    " + bytes.hex());
    }

    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        super.onClosing(webSocket, code, reason);
        Log.e("webSocketd:", "onClosing    " + code + "  " + reason);
    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        super.onClosed(webSocket, code, reason);
        Log.e("webSocketd:", "onClosed    " + code + "  " + reason);
    }

    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        super.onFailure(webSocket, t, response);
        Log.e("webSocketd:", "onFailure    " + t.toString());
        if (needRestart) {
            SystemClock.sleep(1000);
            if (simpleCallBack != null) {
                simpleCallBack.sure();
            }
        }
    }

    public void close() {
        needRestart = false;
    }
}
